package cn.darkhorse.device.service.impl;

import cn.darkhorse.common.constant.AuditTypeEnum;
import cn.darkhorse.common.constant.CommonConstant;
import cn.darkhorse.device.mapper.MesSpareInboundInfoMapper;
import cn.darkhorse.device.pojo.MesSpareInboundInfo;
import cn.darkhorse.device.pojo.MesSpareParts;
import cn.darkhorse.device.pojo.MesSpareType;
import cn.darkhorse.device.service.MesMsgAuditService;
import cn.darkhorse.device.service.MesSpareInboundInfoService;
import cn.darkhorse.device.service.MesSparePartsService;
import cn.darkhorse.device.service.MesSpareTypeService;
import cn.darkhorse.device.vo.MesSpareInboundInfoVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 备件入库表 服务实现类
 * </p>
 *
 * @author maoguanjun
 * @since 2022-11-11
 */
@Service
public class MesSpareInboundInfoServiceImpl extends ServiceImpl<MesSpareInboundInfoMapper, MesSpareInboundInfo> implements MesSpareInboundInfoService {
    @Resource
    private MesSpareInboundInfoMapper mesSpareInboundInfoMapper;
    @Resource
    private MesSparePartsService mesSparePartsService;
    @Resource
    private MesSpareTypeService mesSpareTypeService;
    @Resource
    private MesMsgAuditService mesMsgAuditService;
    /**
     * 列表
     *
     * @param spareName
     * @param typeName
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public List<MesSpareInboundInfoVO> listAll(String spareName, String typeName, String beginTime, String endTime,String orderType) {
        if(StringUtils.isBlank(orderType)){
            orderType=CommonConstant.ORDER_TYPE_DESC;
        }
        return mesSpareInboundInfoMapper.listAll(spareName,typeName,beginTime,endTime,orderType);
    }

    /**
     * 保存
     *
     * @param mesSpareInboundInfo
     * @return
     */
    @Override
    public boolean saveSpareInbound(MesSpareInboundInfo mesSpareInboundInfo) {
        if(null!=mesSpareInboundInfo.getId()){
            if(!allowModify(mesSpareInboundInfo.getId())){
                throw new ServiceException("只有待审数据可以修改！");
            }
            mesSpareInboundInfo.setGmtModify(LocalDateTime.now());
        }
        return saveOrUpdate(mesSpareInboundInfo);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        if(!allowModify(id)){
            throw new ServiceException("只有待审数据可以删除！");
        }
        //删除未审已删除消息
        mesMsgAuditService.removeMsg(id,AuditTypeEnum.SPARE_PARTS_INBOUND.getType());
        return removeById(id);
    }

    /**
     * 提交
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submit(Long id) {
        MesSpareInboundInfo info=getById(id);
        if(null!=info){
            info.setAuditStatus(CommonConstant.AUDIT_STATUS_UNDER_REVIEWED);
            //发送审核消息
            mesMsgAuditService.sendAuditMsg(AuditTypeEnum.SPARE_PARTS_INBOUND.getTypeName(), info.getId(), null);
            return updateById(info);
        }
        return false;
    }

    /**
     * 审核
     *
     * @param id
     * @param approver
     * @param approveTime
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean audit(Long id, Long approver, String approveTime,Integer auditStatus) {
        MesSpareInboundInfo info=getById(id);
        if(null!=info&&CommonConstant.AUDIT_STATUS_UNDER_REVIEWED.equals(info.getAuditStatus())){
            if(auditStatus.equals(CommonConstant.AUDIT_STATUS_REVIEWED)){
                info.setApprover(approver);
                info.setApproveTime(LocalDateTime.parse(approveTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                mesSparePartsService.upInventory(info.getSpareId(),info.getInboundQuantity(), BigDecimal.ZERO,true,CommonConstant.SPARE_APPLY_TYPE_ADD);
            }else{
                info.setApprover(approver);
                info.setApproveTime(LocalDateTime.parse(approveTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            }
            info.setAuditStatus(auditStatus);
            //更新审核通知
            mesMsgAuditService.upAuditMsg(id,approver,approveTime,auditStatus,AuditTypeEnum.SPARE_PARTS_INBOUND.getType());
            return  updateById(info);
        }else{
            throw new ServiceException("非法操作！");
        }
    }

    /**
     * 弃审
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public boolean waiver(Long id) {
        MesSpareInboundInfo info=getById(id);
        if(null!=info){
            MesSpareParts parts=mesSparePartsService.getById(info.getSpareId());
            if(parts.getInventory().compareTo(info.getInboundQuantity())==-1){
                throw new ServiceException("弃审数量大于当前库存量!");
            }else{
                info.setAuditStatus(CommonConstant.AUDIT_STATUS_PENDING_TRIAL);
                info.setApprover(null);
                info.setApproveTime(null);
                mesSparePartsService.upInventory(info.getSpareId(),info.getInboundQuantity(),BigDecimal.ZERO,false,CommonConstant.SPARE_APPLY_TYPE_ADD);
                return updateById(info);
            }
        }
        return false;
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @Override
    public MesSpareInboundInfoVO view(Long id) {
        MesSpareInboundInfoVO vo=new MesSpareInboundInfoVO();
        MesSpareInboundInfo info=getById(id);
        if(null!=info){
            BeanUtils.copyProperties(info,vo);
            MesSpareParts mesSpareParts=mesSparePartsService.getById(info.getSpareId());
            if(null!=mesSpareParts) {
                vo.setSpareModel(mesSpareParts.getModel());
                vo.setTypeId(mesSpareParts.getType());
                vo.setSpareUnit(mesSpareParts.getUnit());
                vo.setSpareName(mesSpareParts.getName());
                MesSpareType type = mesSpareTypeService.getById(mesSpareParts.getType());
                vo.setTypeName(null!=type?type.getName():"");
                vo.setInventory(mesSpareParts.getInventory());
            }
        }else{
            throw new ServiceException("该业务单据已删除!");
        }
        return vo;
    }

    /**
     * 是否存在入库记录
     *
     * @param spareId
     * @return
     */
    @Override
    public boolean recordExists(Long spareId) {
        LambdaQueryWrapper<MesSpareInboundInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(MesSpareInboundInfo::getSpareId,spareId);
        Long count=count(wrapper);
        if(count>0){
            return  true;
        }
        return false;
    }

    /**
     * 是否允许修改
     * @param id
     * @return
     */
    boolean  allowModify(Long id){
        MesSpareInboundInfo info=getById(id);
        if(null!=info){
            if(CommonConstant.AUDIT_STATUS_PENDING_TRIAL.equals(info.getAuditStatus())){
                return true;
            }
        }
        return false;
    }

}
